﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessComponents.DomainModels;
using System.Data.Objects;
using System.Data.Common;

namespace BusinessComponents.DAL
{
    class ReportDaoImpl : IReportDao
    {
        private WaterFrontEntities wfe;

        public IEnumerable<Report1001> fetchRpt1001(int numOfRecords)
        {
            using (wfe = new WaterFrontEntities())
            {
                string query = " select c.UserName, sum(r.TotalCost) as Amt " +
                               " from WaterFrontEntities.ReservationMasters as r " +
                               " join WaterFrontEntities.Customers as c " +
                               " on r.UserId == c.UserId " +
                               " where r.IsCancelled == 0 " +
                               " group by c.UserName " +
                               " order by Amt desc limit " + numOfRecords;

                ObjectQuery<DbDataRecord> records = wfe.CreateQuery<DbDataRecord>(query);

                List<Report1001> list = new List<Report1001>();

                foreach (var r in records)
                {
                    string name = r[0].ToString();
                    double amt = (int)r[1] / 100.00;
                    list.Add(new Report1001(name, amt));
                }

                return list;
            }
        }

        public IEnumerable<Report2001> fetchRpt2001()
        {
            using (wfe = new WaterFrontEntities())
            {
                string query = " select c.Description, sum(r.DayCount) as Days " +
                               " from WaterFrontEntities.ReservationMasters as r " +
                               " right join WaterFrontEntities.RoomTypes as c " +
                               " on r.TypeCode == c.TypeCode " +
                               " where r.IsCancelled == 0 " +
                               " group by c.Description " +
                               " order by Days desc ";

                ObjectQuery<DbDataRecord> records = wfe.CreateQuery<DbDataRecord>(query);

                List<Report2001> list = new List<Report2001>();
                foreach (var r in records)
                {
                    string desc = r[0].ToString();
                    int days = (int)r[1];
                    list.Add(new Report2001(desc, days));
                }

                return list;
            }
        }
    }
}
